import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shop/config/config.dart';
import 'package:shop/http/HttpUtil.dart';

class RefundDetailPage extends StatefulWidget {
  RefundDetailPage({Key key, this.reid}) : super(key: key);

  final String reid;

  @override
  _RefundDetailPage createState() => _RefundDetailPage(reid);
}

class _RefundDetailPage extends State<RefundDetailPage> {
 // List goods = [{"gid":100,"gname":"苹果","gprice":100},{"gid":101,"gname":"苹果1","gprice":200},{"gid":102,"gname":"苹果2","gprice":300},{"gid":103,"gname":"苹果3","gprice":400},];
  var reid;   // 退款退货编号
  var item;   // 退货详情
  TextEditingController _express_numbers = new TextEditingController();
  // 构造函数
  _RefundDetailPage(id){
    this.reid=id;
    _onLoading();
  }
  // 加载数据的函数
  void _onLoading() async{
    var x = await HttpUtil().get("/app/refunds/selectRefundById?id="+reid);
    if(x['code']==200){
      item=x['data'];
      setState(() {

      });
    }

  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context , width: 464 , height: 800) ;
    return Scaffold(
      appBar: AppBar(
        title: Text("退款/退货详情"),
      ),
      body: new SingleChildScrollView(
        physics: BouncingScrollPhysics(),
          child: Container(
        padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
        child: Column(
          children: [
            Container(
              // height: ScreenUtil().setHeight(100),
              width: double.infinity,
              margin: EdgeInsets.all(ScreenUtil().setWidth(5)),
              padding: EdgeInsets.symmetric(vertical: ScreenUtil().setHeight(5),horizontal: ScreenUtil().setHeight(10)),
              decoration: BoxDecoration(
                  color: Colors.white
              ),
              child: Column(
                children: [
                  Row(children: [
                    Icon(Icons.check_circle,color: Colors.orange,),
                    // Text("退款成功")
                    _showRefundStatus()
                  ],),
                  Row(children: [
                    // Text(item['refund_sqTime'])
                    _showRefundTime()
                  ],)
                ],
              ),
            ),
            SizedBox(height: ScreenUtil().setWidth(10),),
            Container(
              margin: EdgeInsets.all(ScreenUtil().setHeight(10)),
              padding: EdgeInsets.all(ScreenUtil().setHeight(10)),
              width: double.infinity,
              decoration: BoxDecoration(
                color: Colors.white
              ),
              child: Column(
                children: [
                  Row(children: [Text("退款信息",style: TextStyle(fontWeight: FontWeight.bold),)],),
                  SizedBox(height: ScreenUtil().setHeight(10),),
                  Row(
                    children: [
                      // CachedNetworkImage(imageUrl: "https://www.itying.com/images/flutter/list2.jpg",width: ScreenUtil().setHeight(80),height: ScreenUtil().setHeight(80),),
                      CachedNetworkImage(imageUrl: Config.getInstance().getHost()+item['goods']['gimages'],width: ScreenUtil().setHeight(80),height: ScreenUtil().setHeight(80),),
                      SizedBox(width: ScreenUtil().setWidth(10),),
                      Column(
                        children: [
                          Text(item['goods']['gname']),
                          Text(item['orderDetail']==null || item['orderDetail']['guiges']==null ? " " :item['orderDetail']['guiges'],style: TextStyle(color: Colors.black38),),
                        ],
                      )
                    ],
                  ),
                  SizedBox(height: ScreenUtil().setHeight(10),),
                  Row(
                    children: [
                      Container(
                        child: Text("退款原因"),
                        width: ScreenUtil().setWidth(80),
                      ),
                      SizedBox(width: ScreenUtil().setWidth(10),),
                      // Text("不喜欢，不爱了"),
                      _showRefundReason(item['refund_reason'])
                    ],
                  ),
                  SizedBox(height: ScreenUtil().setHeight(10),),
                  Row(
                    children: [
                      Container(
                        child: Text("退款金额"),
                        width: ScreenUtil().setWidth(80),
                      ),
                      SizedBox(width: ScreenUtil().setWidth(10),),
                      Text("￥${item['order']['tuikuanMoney']}"),
                    ],
                  ),
                  SizedBox(height: ScreenUtil().setHeight(10),),
                  Row(
                    children: [
                      Container(
                        child: Text("申请件数"),
                        width: ScreenUtil().setWidth(80),
                      ),
                      SizedBox(width: ScreenUtil().setWidth(10),),
                      Text(item['refund_number'].toString()),
                    ],
                  ),
                  SizedBox(height: ScreenUtil().setHeight(10),),
                  Row(
                    children: [
                      Container(
                        child: Text("照片凭证"),
                        width: ScreenUtil().setWidth(80),
                      ),
                      SizedBox(width: ScreenUtil().setWidth(10),),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          item['picture1']==null?Text(""):  CachedNetworkImage(imageUrl: Config.getInstance().getHost()+"upload/tuiHuanHuoImages/"+item['picture1'],width: ScreenUtil().setHeight(80),height: ScreenUtil().setHeight(80),),
                          item['picture2']==null?Text(""):  CachedNetworkImage(imageUrl: Config.getInstance().getHost()+"upload/tuiHuanHuoImages/"+item['picture2'],width: ScreenUtil().setHeight(80),height: ScreenUtil().setHeight(80),),
                          item['picture3']==null?Text(""):  CachedNetworkImage(imageUrl: Config.getInstance().getHost()+"upload/tuiHuanHuoImages/"+item['picture3'],width: ScreenUtil().setHeight(80),height: ScreenUtil().setHeight(80),),
                        ],
                      )
                    ],
                  ),
                  SizedBox(height: ScreenUtil().setHeight(10),),
                  Row(
                    children: [
                      Container(
                        child: Text("申请时间"),
                        width: ScreenUtil().setWidth(80),
                      ),
                      SizedBox(width: ScreenUtil().setWidth(10),),
                      Text(item['refund_sqTime']),
                    ],
                  ),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.all(ScreenUtil().setHeight(10)),
                  padding: EdgeInsets.all(ScreenUtil().setHeight(10)),
                  width: double.infinity,
                  decoration: BoxDecoration(
                      color: Colors.white
                  ),
                  child:
                      item['refund_type']==1 ?
                  Column(
                    children: [
                      Row(children: [Text("发货信息",style: TextStyle(fontWeight: FontWeight.bold),)],),
                      SizedBox(height: ScreenUtil().setHeight(5),),
                      // Row(children: [
                        Text(item['admin_address']),
                      // ],),
                      Row(children: [
                        Expanded(child: Text('可选快递种类：'+item['appoint_express_type']))
                      ]),
                      Row(
                        children: [
                          Text("快递单号"),
                          item['user_express_numbers']==null || item['user_express_numbers']=='' ?
                          Expanded(child: TextField(
                            controller: _express_numbers,
                            onChanged: (value){
                              _express_numbers.text=value;
                              final length=value.length;
                              _express_numbers.selection = TextSelection(baseOffset: length, extentOffset: length);
                            },
                          )
                          ):Text(item['user_express_numbers'])
                        ],
                      ),
                      item['user_express_numbers']==null || item['user_express_numbers']=='' ?
                      Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            GestureDetector(
                                child:Container(
                                  margin: EdgeInsets.only(top: 5),
                                  width:80,
                                  height: 30,
                                  child: Center(child:Text("确定",style: TextStyle(color: Colors.white),)),
                                  decoration: BoxDecoration(
                                    color: Colors.deepOrange,
                                    borderRadius: BorderRadius.all(Radius.circular(20.0)),
                                  )
                                ),onTap: () async {
                                  print("确定");
                                  print(item['id']);
                                  print(_express_numbers.text);
                                  var x = await HttpUtil().get("/app/refunds/userSubmitShouFahuoInfo?id=${item['id']}&kuaididanhao=${_express_numbers.text.trim()}");
                                  if(x['code']==200){
                                    Fluttertoast.showToast(
                                        msg: "填写成功",
                                        toastLength: Toast.LENGTH_SHORT,
                                        gravity: ToastGravity.BOTTOM,
                                        timeInSecForIosWeb: 2,
                                        backgroundColor: Colors.black45,
                                        textColor: Colors.white,
                                        fontSize: 16.0
                                    );
                                    setState(() {
                                      item['user_express_numbers']=_express_numbers.text;
                                    });
                                  }else{
                                    Fluttertoast.showToast(
                                        msg: x['msg'],
                                        toastLength: Toast.LENGTH_SHORT,
                                        gravity: ToastGravity.BOTTOM,
                                        timeInSecForIosWeb: 2,
                                        backgroundColor: Colors.black45,
                                        textColor: Colors.white,
                                        fontSize: 16.0
                                    );
                                  }
                              },
                            )
                          ]
                      ): Text('')
                    ],) : null,
                )
              ],
            ),
          ),
      )
    );
  }
  // 动态显示售后类型
  Text _showRefundType(type) {
    var s = "退款";   // 默认退款
    if(type==1)
      s="退货";
    else if(type==2)
      s="换货";
    else if(type==3)
      s="退款不退货";
    else if(type==4)
      s="取消订单";
    return Text(s);
  }
  // 动态显示售后状态
  Text _showRefundStatus() {
    var status=item==null ? 1:item['refund_status'];
    var express_number=item==null ? '':item['user_express_numbers'];
    var s = "申请中"; // 默认
    if(status==0){
      s='等待商家审核';
    }else if(status==1)
      s="申请未通过";
    else if(status==2){
      if(express_number==null || express_number==''){
        s='审核通过';
      }else{
        s='等待商家收货';
      }
    }
    else if(status==3){
     s='商家已收到货';
    }
    else if(status==4)
      s="商家发货";
    else if(status==5)
      s="客户收货";
    else if(status==6)
      s="退款成功";
    else if(status==7)
      s="退货成功";
    return Text(s);
  }
  // 动态显示退款原因
  Text _showRefundReason(reason) {
    var s = "其他";   // 默认退款
    if(reason==1)
      s="七天无理由退货";
    else if(reason==2)
      s="产品质量问题";
    else if(reason==3)
      s="发票问题";
    else if(reason==4)
      s="不喜欢了/不爱了";
    else if(reason==5)
      s="拍错了/看错了";
    else if(reason==6)
      s="商家没发货";
    return Text(s);
  }
  // 动态显示售后状态
  Text _showRefundTime() {
    var s = "申请中";   // 默认
    var status= item==null ? 1:item['refund_status'];
    if(item==null){
      return Text('');
    }else if(status==1)
      s=item==null ? '': item['refund_disagree_reason'];
    else if(status==2)
    {
      var t;
      t= item['user_fahuo_time']==null? "":item['user_fahuo_time'];
      if(item['user_express_numbers']==null || item['user_express_numbers']==''){
        s='请发货给商家';
      }else{
        s="寄货时间:"+t;
      }
    }
    else if(status==3)
      s="商家已收货";
    else if(status==4)
     {
       var t;
       t= item['admin_fahuo_time']==null? "":item['admin_fahuo_time'];
       s="换货已发"+t;
     }
    else if(status==5)
      s="已收货，请到订单页面确认收货";
    else if(status==6)
      s=item['order']['orFinishtTime']==null? "":item['order']['orFinishtTime'];
    else if(status==7)
      s=item['order']['orFinishtTime']==null? "":item['order']['orFinishtTime'];
    return Text(s);
  }
}